﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RecipeBuildUnits
{
    public partial class frmIng : Form
    {
        
        
        
        public frmIng()
        {
            InitializeComponent();
        }

        private void frmIng_Load(object sender, EventArgs e)
        {
            SqlConnection conn = Utils.getConnection();
            DataTable dt;
            if (conn != null)
            {
                dt = Utils.getDataTable(conn, "IngType");
                comboBox1.DataSource = dt;
                comboBox1.DisplayMember = dt.Columns[1].ToString();
                comboBox1.ValueMember = dt.Columns[0].ToString();
            } 
            else
            {
                MessageBox.Show("Connection Error");
               
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection conn = Utils.getConnection();
            
            if (conn != null && comboBox1.SelectedIndex != 0)
            {
                int selectedIng = int.Parse(comboBox1.SelectedValue.ToString());
                SqlCommand cmd = new SqlCommand("Select * from Ingridient where IngTypeID = " + selectedIng , conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "Ingridient");
                dataGridView1.DataSource = ds.Tables[0];
            }     
               
            }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String name = textBox1.Text;
            String unit = textBox2.Text;
            int a = int.Parse(comboBox1.SelectedValue.ToString());

             SqlConnection conn = Utils.getConnection();
            SqlCommand cmd = new SqlCommand("Insert into Ingridient values '"+name+"','"+unit+"',"+a , conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            MessageBox.Show("Them Thanh Cong");
            
        }
                 
        }
    }

